/**
 * table history.
 */
;(function (history) {
	window.dataTable = null;
	var hashUrl = {
			"search": null,
			"sort1": null,
			"sort2": null,
			"page": 1
		};
	if(!location.hash)
		location.replace(location.href + "#1");
	
	//高级搜索代理函数，返回false不执行默认搜索代码。
	window.advSearchProxy = function (values)
	{
		values = values.split("%2F");
		if(values.length > 1)
		{
			values = values.join("");
			showMsg({
				message: "您的搜索条件中含有字符\"/\"将无法进行搜索，所以系统自动忽略了此字符。",
				type: "exTip"
			});
		}
		operation({search: "advsearch/" + values, page: 1}, true);
		return false;
	};
	
	//排序代理函数
	window.sortProxy = function (field, method)
	{
		operation({sort: field + "-" + method});
		return false;
	};

	//分页代理函数
	window.setPageProxy = function (page)
	{
		operation({page: page});
		return false;
	};

	//基于锚点判变的搜索
	window.search = function (value)
	{
		value = value.split("/");
		if(value.length > 1)
		{
			value = value.join("");
			$("#searchValue").val(value);
			showMsg({
				message: "您的搜索条件中含有字符\"/\"将无法进行搜索，所以系统自动忽略了此字符。",
				type: "exTip"
			});
		}
		operation({search: "search/" + value, page: 1}, true);
	};

	//根据操作生成相应锚点
	window.operation = function (code, reset) {
		if(reset)
			hashUrl = code;
		if(typeof code.search == "string")
			hashUrl.search = code.search;
		if(code.page)
			hashUrl.page = code.page;
		if(typeof code.sort == "string")
			hashUrl.sort1 = code.sort;
		location.hash = ((hashUrl.search && hashUrl.search.length > 0) ? hashUrl.search + "/" : "") + ((hashUrl.sort1 && hashUrl.sort1.length > 0) ? hashUrl.sort1 + "-" : "") + (hashUrl.page ? hashUrl.page : 1);
	};
	
	var enabled = true;
	window.disableTableHistory = function () {
		enabled = false;
	};
	
	window.enableTableHistory = function () {
		enabled = true;
		historyChange((location.hash && location.hash.length > 0) ? location.hash.substr(1) : "");
	};

	var tableCreator;
	window.setTableCreator = function (fun) {
		tableCreator = fun;
	};
	
	//为hash变化添加一个回调方法
	function historyChange(hash) {
		if(enabled == false)
			return;
		var url = hash.split("/");
		switch(url[0])
		{
		case "search":
			hashUrl.search = "search/" + url[1];
			if(!dataTable)
				dataTable = tableCreator();
			dataTable.resetTable();
			dataTable.search(url[1]);
			//排序处理
			if(parseInt(url[2]))
			{
				dataTable.cleanSortEffect();
				dataTable.setPage(url[2]);
				hashUrl.page = url[2];
			}
			else
			{
				var pageTag = url[2].split("-");
				hashUrl.sort1 = pageTag[0] + "-" + pageTag[1];
				hashUrl.page = pageTag[2];
				dataTable.sort1(pageTag[0], pageTag[1]);
				dataTable.setPage(pageTag[2]);
			}
			break;
		case "advsearch":
			hashUrl.search = "advsearch/" + url[1];
			if(!dataTable)
				dataTable = tableCreator();
			dataTable.resetTable();
			dataTable.advSearch(url[1]);
			if(parseInt(url[2]))
			{
				dataTable.cleanSortEffect();
				hashUrl.page = url[2];
				dataTable.setPage(url[2]);
			}
			else
			{
				var pageTag = url[2].split("-");
				hashUrl.sort1 = pageTag[0] + "-" + pageTag[1];
				hashUrl.page = pageTag[2];
				dataTable.sort1(pageTag[0], pageTag[1]);
				dataTable.setPage(pageTag[2]);
			}
			break;
		default:
			if(!dataTable)
				dataTable = tableCreator();
			dataTable.resetTable();
			dataTable.list();
			hashUrl.search = null;
			if(parseInt(url[0]))
			{
				dataTable.cleanSortEffect();
				hashUrl.page = url[0];
				dataTable.setPage(url[0]);
			}
			else
			{
				var pageTag = url[0].split("-");
				hashUrl.sort1 = pageTag[0] + "-" + pageTag[1];
				hashUrl.page = pageTag[2];
				dataTable.sort1(pageTag[0], pageTag[1]);
				dataTable.setPage(pageTag[2]);
			}
			break;
		}
	};
	
	history.pushCallback(historyChange);
})(history);